<?php
/**
 * @authors	sachin,shine,jonia
 * @copyright   CMM Technologies
 * @date        11-11-2011
 * @file	User.cls.php
 * @description manipulation of common functions related to Users are handled here.
 */

 class User extends Base
 {

	/**
     * User id
     * @var integer
     */
	 private $id;

	/**
     * user name
     * @var string
     */
	 private $userName;

	/**
     * First name
     * @var string
     */
	 private $firstName;

	/**
     * Last name
     * @var string
     */
	 private $lastName;

	/**
     * email 
     * @var string
     */
	 private $email;

	/**
     * Designation Id
     * @var integer
     */
	 private $designationId;

	/**
     * User type
     * @var string
     */
	 private $userType;

	/**
     * status
     * @var string
     */
	 private $status;

	/**
     * Last login
     * @var date
     */
	 private $lastLogin;

	 public function __construct($id)
	 {
		parent::init();

		$this->id=$id;

		$data=self::$db->getDBContentsBySql("select * from tms_users where id='$id'");
		
		$this->userName=$data[0]['username'];
		$this->firstName=$data[0]['first_name'];
		$this->lastName=$data[0]['last_name'];
		$this->email=$data[0]['email'];
		$this->designationId=$data[0]['designation_id'];
		$this->userType=$data[0]['user_type'];
		$this->status=$data[0]['status'];
		$this->lastLogin=$data[0]['last_login'];
	 }
	
	/**
     * Function to get the first name
	 *return string first name 
	 */
	 public function getFirstName()
	 {
		 return $this->firstName;
	 }
		

	/**
     * Function to get the last name
	 *return string last name 
	 */
	  public function getLastName()
	 {
		 return $this->lastName;
	 }

	
	/**
     * Function to get the email
	 *return string email  
	 */
	  public function getEmail()
	 {
		 return $this->email;
	 }


	/**
     * Function to get the user name
	 *return string user name
	 */
	  public function getUserName()
	 {
		 return $this->userName;
	 }

  
	/**
     * Function to get the designation id
	 *return integer designation id
	 */
	  public function getDesignationId()
	 {
		 return $this->designationId;
	 }

	 
	/**
     * Function to get the user type
	 *return string user type
	 */
	  public function getUserType()
	 {
		 return $this->userType;
	 }

	 
	/**
     * Function to get the status
	 *return string user status
	 */
	 public function getStatus()
	 {
		 return $this->status;
	 }

	  
	/**
     * Function to get the last login
	 *return date user last login
	 */
	 public function getLastLogin()
	 {
		 return $this->lastLogin;
	 }


	/**
     * Function to get all project that the user is member in
	 * return array contains list of projects
	 */
	public function getProjects()
	 {
		$id=$this->id;
		$result=self::$db->getDBFieldByCond("tms_project_members","projects_id","users_id=$id");
		return $result;
	 }
		

	/**
     * Function to get all task in a particular project that the user is member in
	 * @param integer project id
	 * return array contains list of task
	 */
	public function getTaskByProject($projectid,$order="task_name",$desc=false,$filter,$module="",$status="",$priority="")
	 {
		$id=$this->id;
		$newfilter['task_name']=$filter;
		$newfilter['task_description']=$filter;
			
		$cond= "assignee_id='$id' AND project_modules_id= ANY (SELECT id FROM tms_project_modules WHERE projects_id='$projectid')";

		if($module!="")
			$cond.="AND project_modules_id='$module'";		
		if($status!="")
			$cond.="AND status='$status'";
		if($priority!="")
			$cond.="AND priority='$priority'";

		$result=self::$db->getDBFieldByCond("tms_task","id",$cond,$order,$desc,$newfilter);
		return $result;

	 }

	
	/**
     * Function to get all task in a particular module that the user is member in
	 * @param integer module id
	 * return array contains list of task
	 */
	 public function getTaskByModule($moduleid)
	 {
		$id=$this->id;
			
		$cond= "assignee_id='$id' AND project_modules_id= $moduleid";
		$result=self::$db->getDBFieldByCond("tms_task","id",$cond);
		return $result;
	 }
	 

	 /**
	 * addUser
	 *
     * A function to add a user
	 *@param int $username
	 *@param string $memberRole
	 *@return boolean - true if successful or false on failure
	 */

	 public static function addUser($username,$passhash,$fname,$lname,$email,$designation,$usertype)
	 {
		 parent::init();

		 $fields = "username,password,first_name,last_name,email,designation_id,user_type";
		 $valuesArr = array($username,$passhash,$fname,$lname,$email,$designation,$usertype);
		 $status = self::$db->dbInsert("tms_users",$fields,$valuesArr);

		 if($status==0||$status=="Error Occured")
			 return false;
		 else 
			 return true;

	 }

	 /**
	 * getNameById
	 *
     * A function to get the name of a user
	 * given the id
	 *@param int $id
	 *@return string - the name of user
	 */

	 public static function getNameByID($id)
	 {
		 parent::init();

		$result=self::$db->getCellContentByCondition("tms_users","concat(first_name,' ',last_name)","id=$id");
		return $result;

	 }


	 /**
	 * getUsers()
	 *
     * A function to get the list of users
	 *@return array - list of users
	 */

	 public static function getUsers()
	 {
		parent::init();

		$arr = self::$db->getDBFieldByCond("tms_users","id","true");
		return $arr;


	 }
	/**
	*editProfile()
	*
	*A function to edit first name,last name and email id
	*@param $firstname, firstname for update
	*@param $lastname, last name for update
	*@param $email, email for update
	*return boolean, true for sucess and false for failure
	*/
	public  function editProfile($firstname,$lastname,$email)
	 {
		
		$id=$this->id;
		$field= "first_name = '$firstname',last_name = '$lastname',email = '$email' WHERE `id` = '$id'";
		$edit= self::$db->dbUpdate("tms_users",$field);
		return $edit;

	 }
	

	/**
	*changePassword()
	*
	*A function to change the current password to new password
	*@param $old, old password
	*@param $new,new password
	*return boolean true for sucess and false for failure
	*/
	public function changePassword($old,$new)
	{
		
		$id=$this->id;
		$dbold=self::$db->getCellContentByCondition("tms_users","password","id=$id");
		$field="password = '$new' WHERE id = '$id'";
		if($old==$dbold)
		{
			$change=self::$db->dbUpdate("tms_users",$field);
			return $change;
		}
		else
			return false;
	 }

 public static function getDesignation()
	 {
		parent::init();
		$result = self::$db->getDBFieldByCond("tms_designation","id","true");
		return $result;
	 }


	  public static function deleteUser($id)
	 {
		 parent::init();
		$qry="DELETE FROM tms_users WHERE id='$id'";
		$result=self::$db->executeQuery($qry);
		return $result;

	 }
 

 }

?>